class Solution {
    public:
        int totalFruit(vector<int>& fruits) {
            if(fruits.size()==1)return 1;
            int i=0;
            int kinds=0;
            int maxn=0;
            vector<int> count(fruits.size());
            for(int j=0;j<fruits.size();j++){
                if(count[fruits[j]]==0){
                    kinds++;
                }
                count[fruits[j]]++;
                while(kinds>2){
                    count[fruits[i]]--;
                    if(count[fruits[i]]==0){
                        kinds--;
                    }
                    i++;
                }
                maxn=max(maxn,j-i+1);
            }
            return maxn;
        }
    };